<?php
declare (strict_types=1);

namespace app\course\service\cert\admin;

use app\admin\logic\system\Config as SystemConfig;
use app\common\logic\Attach;
use app\common\service\BaseService;
use app\course\logic\cert\admin\CourseCert;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 证书
 */
class Cert extends BaseService
{
    /**
     * @Inject()
     *
     * @var CourseCert
     */
    protected $courseCertLogic;

    /**
     * 课程学员证书
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-07
     */
    public function courseStudent(int $courseId)
    {
        $map = [];
        // 查询当前课程已经学完的所有学员
        // $students = app(LearnCourseFinished::class)->getStudentList($map, $courseId, Request::get('phone/s', ''))->bindAttr('user', ['avatar_url', 'nick_name', 'phone'])->hidden(['user']);
        $phone    = Request::get('phone/s', '');
        $students = $this->courseCertLogic->getStudentList($courseId, $map, $phone)->bindAttr('user', ['avatar_url', 'nick_name', 'phone'])->hidden(['user']);
        // 定义字段
        $keyList = [
            'user'        => ListItem::make('custom')->title('用户'),
            'create_time' => ListItem::make()->title('学完时间'),
        ];

        $searchKey = [
            'phone' => FormItem::make('input')->title(false)->placeholder('学员手机号')->maxlength(30),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $students;

        return $list->send();
    }

    /**
     * 证书打印
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-07
     *
     * @return   [type]                         [description]
     */
    public function printData()
    {
        $course_id = Request::get('course_id/d');
        $user_id   = Request::get('user_id/d');

        $cert_info = SystemConfig::get('course.cert.config', [], request()->mhm_id);
        $data      = $this->courseCertLogic->getStudentPrintData($user_id, $course_id, $cert_info['show_sections'])->bindAttr('user', ['nick_name'])->toArray();

        // 标题替换
        $cert_info['title'] = str_replace('{{course_title}}', $data['course_title'], $cert_info['title']);
        // 内容替换
        $cert_info['content'] = str_replace(['{{nick_name}}', '{{date}}', '{{course_title}}', '{{finished_class_hour}}'], [$data['nick_name'], $data['cert_create_time'], $data['course_title'], $data['finished_class_hour']], $cert_info['content']);
        // 公章地址
        if ($cert_info['official_seal'] ?? false) {
            $cert_info['official_seal_url']    = Attach::where('id', $cert_info['official_seal'])->findOrEmpty()->getAttr('fileurl');
            // $cert_info['official_seal_base64'] = $this->imgtobase64($cert_info['official_seal_url']);
        }

        // 背景地址
        if ($cert_info['background'] ?? false) {
            $cert_info['background_url']    = Attach::where('id', $cert_info['background'])->findOrEmpty()->getAttr('fileurl');
            // $cert_info['background_base64'] = $this->imgtobase64($cert_info['background_url']);
        }

        $data['cert_info'] = $cert_info;

        return $this->parseToData($data, 1, '打印成功');
    }

    /**
     * 把网络图片图片转成base64
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/06/24
     *
     * @param string $img 图片地址
     * @return string
     */
    public function imgtobase64($img = '')
    {
        $imageInfo = getimagesize($img);
        return 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents($img)));
    }
}
